using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace StupidSpaceThing
{
	internal class Pylon : Unit
	{
		private bool caught = false;
		public bool Moving = true;
		public Level3_PuzzleGuide InfoText = null;
		private bool infoIsShowing = false;

		public Pylon(Vector3 position)
			: base(new PylonEntity(position)) {
			massModifier = 5;	
		}

		public override void Update(RealGameTime gameTime) {
			if( !Moving ) {
				Entity.ColObject.Body.LinearVelocity = Vector2.Zero;
			}
			if( Game1.Player != null ) {
				float distance = Vector3.Distance(Position, Game1.Player.Position);
				if( Moving ) {
					if( distance < 40 ) {
						ApplyForce(Vector2.Normalize(Position2D - Game1.Player.Position2D) * 100, false);
					}
				}
				if( InfoText != null ) {
					if( distance < 12 && !infoIsShowing ) {
						GuideSystem.ShowPriority(InfoText);
						infoIsShowing = true;
					} else if( distance >= 12 && infoIsShowing ) {
						GuideSystem.ClosePriority();
						infoIsShowing = false;
					}
				}
				if( distance < 6 && !Entity.Dead && !Game1.Player.Died ) {
					caught = true;
					if( Game1.Level == 1 ) {
						Entity.Die();
						LevelSystem.Level1_CatchPylon();
					}
				}				
			}
			Entity.ColObject.Body.ApplyAngularImpulse(2.0f);
			Entity.ColObject.Body.AngularVelocity /= 1.01f;
			Entity.Rotation = Quaternion.CreateFromYawPitchRoll(Entity.ColObject.Body.Rotation, 0, 0);
			Entity.UpdateRotationMatrix();
			base.Update(gameTime);
		}

		public override void Die() {
			if( !caught && Game1.Level == 1 ) {
				caught = true;
				GuideSystem.ShowPriority(new TextGuide("You have to get the file before its faction is eliminated!", Game1.SpriteFont, TextAlign.Both, 1, Color.White, 3000, 
					delegate() { 
						Game1.Game.Reset(); 
					}), true);			
			}
		}
	}
}
